package Controller;

import Controller.Actions.Add.*;
import Controller.Actions.Delete.*;
import Controller.Actions.Modify.*;
import Controller.Actions.Get.*;
import Controller.Actions.GetAll.*;
import Controller.Actions.Search.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Controller.Actions.*;

public class Controller extends HttpServlet {
    private HashMap actions;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String path = request.getRequestURL().toString();

        Action action = findAction(path);
          
        if(action != null)
            action.perform(request, response);
        else
        {
            HttpServletResponse httpResponse=(HttpServletResponse)response;
            httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
        return;
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    public String getServletInfo() 
    {
        return "Controller";
    }
    
    public void init() throws ServletException 
    {
        //Creamos los subcontrollers y los metemos al hashmap.
        actions = new HashMap();
        actions.put("Validate", new ValidateAction());
        actions.put("Login", new LoginAction());
        actions.put("AddStudent", new AddStudentAction());
        actions.put("AddClassroom", new AddClassroomAction());
        actions.put("AddProfessor", new AddProfessorAction());
        actions.put("AddCourse", new AddCourseAction());
        actions.put("AddSchedule", new AddScheduleAction());
        actions.put("DeleteStudent", new DeleteStudentAction());
        actions.put("DeleteClassroom", new DeleteClassroomAction());
        actions.put("DeleteProfessor", new DeleteProfessorAction());
        actions.put("DeleteCourse", new DeleteCourseAction());
        actions.put("DeleteSchedule", new DeleteScheduleAction());
        actions.put("ModifyStudent", new ModifyStudentAction());
        actions.put("ModifyClassroom", new ModifyClassroomAction());
        actions.put("ModifyProfessor", new ModifyProfessorAction());
        actions.put("ModifyCourse", new ModifyCourseAction());
        actions.put("GetStudent", new GetStudentAction());
        actions.put("GetClassroom", new GetClassroomAction());
        actions.put("GetProfessor", new GetProfessorAction());
        actions.put("GetCourse", new GetCourseAction());
        actions.put("GetAllStudents", new GetAllStudentsAction());
        actions.put("GetAllClassrooms", new GetAllClassroomsAction());
        actions.put("GetAllProfessors", new GetAllProfessorsAction());
        actions.put("GetAllCourses", new GetAllCoursesAction());
        actions.put("SearchStudentSchedule", new SearchStudentScheduleAction());
        actions.put("SearchProfessorSchedule", new SearchProfessorScheduleAction());
        actions.put("SearchStudentPerCourse", new SearchStudentPerCourseAction());
        actions.put("SearchClassroomSchedule", new SearchClassroomScheduleAction());
        actions.put("SearchNotRegisteredCourses", new SearchNotRegisteredCoursesAction());
    }
    
   public Action findAction(String servletPath) 
   {
        int index = servletPath.lastIndexOf('/');
        servletPath = servletPath.substring(index+1, servletPath.length());
        return (Action)actions.get(servletPath);
   }
}